package cn.cai.pethome.base.util;

import lombok.Data;
import lombok.ToString;


@Data
@ToString
public class AjaxResult {

    //私有化构造方法，提供接口
    private AjaxResult(){}

    private Boolean state = true;
    private String message = "操作成功";
    private Object data;


    public static AjaxResult me(){
        return new AjaxResult();
    }


    /**
     * 成功
     * @return
     */
    public static AjaxResult success(){
        return me();
    }

    public static AjaxResult success(Object data){
        AjaxResult me = me();
        me.setData(data);
        return me;
    }

    /**
     * 失败
     * @return
     */
    public static AjaxResult error(){
        return me().setMessage("操作失败").setState(false);
    }
    public static AjaxResult error(String msg){
        return me().setMessage(msg).setState(false);
    }

    //链式编程,这里为什么用private呢？因为调用可能会报空指针异常
    private AjaxResult setState(Boolean state) {
        this.state = state;
        return this;
    }

    private AjaxResult setMessage(String message) {
        this.message = message;
        return this;
    }


}
